{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# C++的函数对象"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 基本概念"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 重载函数调用操作符的类，其对象常称为函数对象。\n",
    "\n",
    "② 函数对象使用重载的()时，行为类似函数调用，也叫仿函数。\n",
    "\n",
    "③ 函数对象(仿函数)是一个类，不是一个函数。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2. 特点"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 函数对象在使用时，可以像普通函数那样调用，可以有参数。\n",
    "\n",
    "② 函数对象超出普通函数的概念，函数对象可以有自己的状态。\n",
    "\n",
    "③ 函数对象可以作为参数传递。\n",
    "\n",
    "④ 仿函数写法是非常灵活的，可以作为参数进行传递。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include<iostream>\n",
    "using namespace std;\n",
    "\n",
    "class MyAdd\n",
    "{\n",
    "public:\n",
    "    int operator()(int v1, int v2)\n",
    "    {\n",
    "        return v1 + v2;\n",
    "    }\n",
    "};\n",
    "\n",
    "//1、函数对象在使用时，可以像普通函数那样调用，可以有参数，可以有返回值\n",
    "void test01()\n",
    "{\n",
    "    MyAdd myAdd;\n",
    "    cout << myAdd(10,10) << endl;\n",
    "}\n",
    "\n",
    "//2、函数对象超出普通函数的概念，函数对象可以有自己的状态\n",
    "class MyPrint\n",
    "{\n",
    "public:\n",
    "    MyPrint()\n",
    "    {\n",
    "        this->count = 0;\n",
    "    }\n",
    "\n",
    "    void operator()(string test)\n",
    "    {\n",
    "        cout << test << endl;\n",
    "        this->count++;\n",
    "    }\n",
    "    int count;  //内部自己状态\n",
    "};\n",
    "\n",
    "void test02()\n",
    "{\n",
    "    MyPrint myPrint;\n",
    "    myPrint(\"hellow world\");\n",
    "    myPrint(\"hellow world\");\n",
    "    myPrint(\"hellow world\");\n",
    "    myPrint(\"hellow world\");\n",
    "\n",
    "    cout << \"myPrint调用次数为：\" << myPrint.count << endl;\n",
    "}\n",
    "\n",
    "//3、函数对象可以作为参数传递\n",
    "\n",
    "void doPrint(MyPrint &mp, string test)\n",
    "{\n",
    "    mp(test);\n",
    "}\n",
    "\n",
    "void test03()\n",
    "{\n",
    "    MyPrint myPrint;\n",
    "    doPrint(myPrint,\"hellow world\");\n",
    "}\n",
    "\n",
    "int main() \n",
    "{\n",
    "    test01();\n",
    "    test02();\n",
    "    test03();\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：\n",
    " - 20\n",
    " - hellow world\n",
    " - hellow world\n",
    " - hellow world\n",
    " - hellow world\n",
    " - myPrint调用次数为：4\n",
    " - hellow world\n",
    " - 请按任意键继续. . ."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
